# ShowErrorsInC-Linux
Shows the stack trace (file name/function name/line number) when a critical error crashes your program.

In C++ in Linux, certain errors, such as division by zero or null pointer reference, cause the program to crash. Using a try/catch block doesn't catch the error. Even catch(...) doesn't catch the error. Your program is going to die. There's no way to stop it. 

But it would be nice to know the file name, function name, and line number where the error happened.

These classes Debug, StackTrace, and error_signals, catch the error signal that the operating system sends to the program just before the program is killed. 

error_signals - A class that sets up error signal handling using the signal command. The error handler tries to display an error message of the type of error and possibly the line number.
signal (SIGFPE, error_signals::Handler); // division by 0
signal (SIGILL, error_signals::Handler); // illegal instruction
signal (SIGSEGV, error_signals::Handler); // bad memory read/write
signal (SIGBUS, error_signals::Handler); // access misalligned memory or non-existent memory
signal (SIGTERM, error_signals::Handler); // Terminate signal from linux kill -SIGTERM <pid>
signal (SIGTSTP, error_signals::Handler); // Terminate signal from keyboard CTRL Z.
signal (SIGINT, error_signals::Handler); // Terminate signal from keyboard CTRL C.

Debug - A class used for debugging. Create an instance of a Debug class at the beginning of each function with Debug debug(__FILE__,__func__,__LINE__) or use the DEBUG_FUNCTION macro. When the variable is created, it adds a row to the StackTrace. When the variable goes out of scope, it removes that row from StackTrace. 

Every few lines, add either debug = __LINE__; or DEBUG_LINE

You can turn on or off stack trace by editing the SHOULD_DEBUG define in file Debug.h.

StackTrace - A class that keeps track of the stack trace in real time. The stack size is fixed. It doesn't allocate or free any memory while it's being used. Class error_signals inherits StackTrace.

main.cpp - A simple test program. Enter a number and it displays 100 / number. You can crash it by pressing CTRL C, CTRL Z, entering 0, or sending a kill comamand from another terminal on the same computer.

makefile - This is a file so you can compile the project using make.

An error message looks like:
Division by zero error
 in file main.cpp in function main in line 33
 in file main.cpp in funcion Function1 in line 44
 in file main.cpp in function Function2 in line 51
 in file main.cpp in function Function3 in line 94
